#include "Menu.h"
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const
{
    cout << "\n###############################\n";
	cout << "########## MAIN MENU ##########\n";
	cout << "###############################\n\n";
    cout << "\tPress\n\t\t1::Display Info\n\t\t2::Read File\n\t\t3::Show Queries\n\t\t4::Find Queries\n\t\t5::Update Queries\n\t\t6::Exit\n"; //this creates the main menu option cout statements

}
void Menu::display_info() const
{
	cout << "\nThis Restaurant Order Management System was developed by:\n\n\tTeam 5 Members:\n\t\tArchie Anugrah (ITB)\n\t\tRico Balakit (TAMU)\n\t\tTravis Lamb (TAMU)\n\n"; //TTL A.2 Have checked it to Mr.Ward. This code need to be located here. --Archie
}

void Menu::read()
{
    int choice;
    string filepath = "";
    //NEED TO FINISH THE IMPLEMENTATION OF THE INTERFACE
	cout << "Which file would you like to read and store? Press:\n";
	cout << "\t1 for catmenu.dat (Categories and Menu Items)\n";
	cout << "\t2 for recipes.dat (Ingredients and Recipes)\n";
	cout << "\t3 for orders.dat (Orders and Order Items)\n";
	cout << "\t4 for all (the .dat files must all be in the same folder as this executable)\n";
	cout << "> ";
    cin >> choice;
    if(!cin)
    {
        cerr << "\n\nERROR bad input.\n\n";
        return;
    }
    if(choice>4 || choice<1)
    {
        cerr << "\n\nERROR bad input.\n\n";
        return;
    }
    if(choice != 4)
    {
        cout << "\nWhat is the filepath of the file (EG data/orders.dat)?\nIf the file is in the same folder as this prgram, leave the field blank.\n";
        cout << "> ";
        cin >> filepath;
        if(!cin)
        {
            cerr << "\n\nERROR bad input.\n\n";
            return;
        }
    }

    switch(choice)
    {
        case 1:
            if(filepath == "")
                filepath = "catmenu.dat";
            read_menu_items(menu_items,categories,filepath); //this starts up the thing to read catmenu.dat
            cout << "Done reading file.\n";
            break;
        case 2:
            if(filepath == "")
                filepath = "recipes.dat";
            readIngredientRecipe(filepath); //Read the ingredient and Recipe data --Archie
            cout << "Done reading file.\n";
            break;
        case 3:
         if(filepath == "")
                filepath = "orders.dat";
            read_orders(orders,filepath);
            //uncomment the following to display
            /*for(int i = 0; i < orders.size();i++)
            {
                orders[i].Order::display();
            }*/
            read_items(order_items,filepath);
            //uncomment the following to display
            /*for(int i = 0; i < order_items.size();i++)
            {
                order_items[i].Order_Item::display();
            }*/
            cout << "Done reading file.\n";
            break;
        case 4:
            read_menu_items(menu_items,categories,"catmenu.dat");
            readIngredientRecipe("recipes.dat");
            read_orders(orders,"orders.dat");
            read_items(order_items,"orders.dat");
            cout << "Done reading files.\n";
            break;
    }
}

void Menu::show() const
{
    //NEED TO FINISH THE IMPLEMENTATION OF THE INTERFACE
	cout << "Show queries \n";


	//Chef
	ShowFindUpdate::PrintMenuItemChef(menu_items,recipes);
	ShowFindUpdate::PrintMenuItemPrice(menu_items);

}
void Menu::find() const
{
	cout << "Find queries \n";
}
void Menu::update()
{
	cout << "Update queries \n";
    categories.push_back(Category(1200,"Soups"));
    categories.push_back(Category(1300,"Salads"));
    categories.push_back(Category(1400,"Sides"));
}

// Read Ingredient Recipe data
// Placed it into ingrdients and recipe vector
// Data Location : filepath to the associated file
// -- Archie
void Menu::readIngredientRecipe(string DataLocation)
{
	//------------Variables dictionary------------

	int Ingr_num; //Total ingredient item
	int Rec_num; //Total recipe item

	//temporary variable to construct ingredient data
	int ingredient_id;
	int recipe_id;
	float amt;
	string units;
	string name;

	//temporary variable to construct recipe data
	int recipe_id2;
	string chef_name;
	string Instruction;

	//------------Algorithm------------

	ingredients.clear(); //clear the current data in ingredients vector
	recipes.clear(); //clear the current data in recipes vector

	//Read the data
	ifstream myfile (DataLocation.c_str()); //Open the data
	if (myfile.is_open())
	{

		/*---------------Ingredient Part-----------------*/
		myfile >> Ingr_num; //read the total ingredient item

		for (int i=0;i<Ingr_num;i++) //get the ingredient data
		{
			myfile >> ingredient_id>> recipe_id>> amt>> units>> name; //parse the line ingredient data
			ingredients.push_back(Ingredient(ingredient_id, recipe_id, amt, units, name));
		}

		/*---------------Recipe Part-----------------*/

		myfile >> Rec_num; //read the total recipe item

		for (int i=0;i<Rec_num;i++) //get the ingredient data
		{

			myfile >>recipe_id2; //read the data
			myfile >>chef_name;

			stringstream tempInstruction; //make temporary stringstream to return the concatenated string
			//Read the instruction text file
			do
			{
				std::getline(myfile, Instruction); //get line and construct the string
				tempInstruction<<Instruction<<'\n';
			} while(Instruction.find('#')==string::npos); //until find # in the line

			recipes.push_back(Recipe(recipe_id2,chef_name,Instructions(tempInstruction.str()))); //add the recipe
		}

		myfile.close();
	}

	else
	{
		cout << "Unable to open \"Recipe and Ingredient\" File"; // if error
	}
}
